<?php
global $_GPC, $_W;
$GLOBALS['frames'] = $this->getMainMenu();
$op = $_GPC['op'] ?? 'display';
if ($op == 'display') {
    $where = "where is_del = 0 and audit_status <> 1 and source_type <> 1 and uniacid =" . $_W['uniacid'];
    if ($_GPC['keywords']) {
        $op = $_GPC['keywords'];
        $where .= " and car_name LIKE  '%$op%' or car_num like '%$op%'";
    }

    $page = max(1, intval($_GPC['page']));
    $size = intval($_GPC['psize']) ? intval($_GPC['psize']) : 10;
    $sql = 'SELECT * FROM ' . tablename('yzzc_sun_goods') . "{$where} ORDER BY id DESC LIMIT " . (($page - 1) * $size) . ',' . $size;

    $info = pdo_fetchall($sql);

    foreach ($info as $item => $value){
        if ($value['source_type'] != 3){
            $store_info = pdo_get('yzzc_sun_branch',['id'=>$value['sid']]);
            $info[$item]['store_name'] = $store_info['name'];
        }
        $info[$item]['spec'] = pdo_getall('yzzc_sun_goods_sku',['goods_id'=>$value['id'],'is_del'=>0],['id','carnum','colour','displacement','num']);
        $info[$item]['createtime'] = date('Y-m-d H:i:s',$value['createtime']);
    }

    $total = pdo_fetchcolumn("select count(*) from " . tablename("yzzc_sun_goods") . $where);
    $pager = pagination($total, $page, $size);

    include $this->template('web/goods_apply');
}
elseif ($op == 'add') {
    $map_key = pdo_get('yzzc_sun_system', array('uniacid' => $_W['uniacid']))['map_key'];
    /**获取省份*/
    $province = pdo_getall('yzzc_sun_city', array('pcode' => 0), array('code', 'fullname'));

    if (checksubmit('submit')) {
        if (empty($_GPC['uid'])) {
            message('会员ID不能为空', '', 'error');
        }

        if (!$user = pdo_get('yzzc_sun_user', ['id' => $_GPC['uid'], 'uniacid' => $_W['uniacid']])) {
            message('会员尚未注册', '', 'error');
        }

        $data = [
            'uid' => $_GPC['uid'],
            'uniacid' => $_W['uniacid'],
            'name' => $_GPC['name'] ?? $user['user_name'],
            'mobile' => $_GPC['mobile'] ?? $user['user_tel'],
            'province' => $_GPC['province'],
            'city' => $_GPC['city'],
            'area' => $_GPC['area'],
            'address' => $_GPC['address'],
            'lng' => $_GPC['lng'],
            'lat' => $_GPC['lat'],
            'id_card_img_front' => $_GPC['id_card_img_front'],
            'id_card_img_side' => $_GPC['id_card_img_side'],
            'pay_status' => 2,
            'createtime' => time()
        ];

        $store = pdo_get('yzzc_sun_store', ['uid' => $_GPC['uid'], 'uniacid' => $_W['uniacid']]);
        if ($store) {
            message('请勿重复申请', $this->createWebUrl('storecar', array()), 'error');
        }

        $result = pdo_insert('yzzc_sun_store', $data);
        if ($result) {
            message('申请成功，等待审核', $this->createWebUrl('storecar', array()), 'success');
        } else {
            message('申请失败', $this->createWebUrl('storecar', array()), 'error');
        }
    }
    include $this->template('web/store/store_add');
}
elseif ($op == 'edit') {
    $map_key = pdo_get('yzzc_sun_system', array('uniacid' => $_W['uniacid']))['map_key'];
    /**获取省份*/
    $province = pdo_getall('yzzc_sun_city', array('pcode' => 0), array('code', 'fullname'));
    $info = pdo_get('yzzc_sun_store', ['uniacid' => $_W['uniacid'], 'store_id' => $_GPC['store_id']]);

    function getcode($code)
    {
        $cityinfo = pdo_get('yzzc_sun_city', array('code' => $code));
        return $cityinfo;
    }

    $info['province_name'] = getcode($info['province'])['fullname'];
    $info['city_name'] = getcode($info['city'])['fullname'];
    $info['area_name'] = getcode($info['area'])['fullname'];
    if (checksubmit('submit')) {
        if (empty($_GPC['uid'])) {
            message('会员ID不能为空', '', 'error');
        }
        if (!$user = pdo_get('yzzc_sun_user', ['id' => $_GPC['uid'], 'uniacid' => $_W['uniacid']])) {
            message('会员尚未注册', '', 'error');
        }

        $data = [
            'uid' => $_GPC['uid'],
            'uniacid' => $_W['uniacid'],
            'name' => $_GPC['name'],
            'mobile' => $_GPC['mobile'],
            'province' => $_GPC['province'],
            'city' => $_GPC['city'],
            'area' => $_GPC['area'],
            'address' => $_GPC['address'],
            'lng' => $_GPC['lng'],
            'lat' => $_GPC['lat'],
            'id_card_img_front' => $_GPC['id_card_img_front'],
            'id_card_img_side' => $_GPC['id_card_img_side'],
            'pay_status' => 2,
            'updatetime' => time()
        ];
        $result = pdo_update('yzzc_sun_store', $data, [
            'store_id' => $_GPC['store_id'],
            'uid' => $_GPC['uid'],
            'uniacid' => $_W['uniacid']
        ]);
        if ($result) {
            message('修改成功', $this->createWebUrl('storecar', array('op' => 'display')), 'success');
        } else {
            message('修改失败', $this->createWebUrl('storecar', array('op' => 'edit', 'store_id' => $_GPC['store_id'])), 'error');
        }
    }
    include $this->template('web/store/store_edit');
}
elseif ($op == 'delete') {
    $store_car = pdo_get('yzzc_sun_goods', array('id' => $_GPC['id']));
    if ($store_car['status'] == 2) {
        message('车辆已出租，不能删除', '', 'error');
    } else {
        $res = pdo_update('yzzc_sun_goods', ['is_del'=>time()],array('id' => $_GPC['id']));
        if ($res) {
            message('操作成功', $this->createWebUrl('storecar', array()), 'success');
        } else {
            message('操作失败', '', 'error');
        }
    }
}
elseif ($op == 'change') {
    $where = ['id' => $_GPC['id']];
    $store_info = pdo_get('yzzc_sun_goods', $where);
    if ($store_info['audit_status'] == $_GPC['status']) {
        message('状态错误，修改失败', $this->createWebUrl('storecar', array()), 'error');
    }

    $data = ['audit_status' => $_GPC['status']];
    if (isset($_GPC['audit_remark']) && $_GPC['audit_remark']) {
        $data['audit_remark'] = $_GPC['audit_remark'];
    }
    if ($_GPC['status'] == 1){
        $data['status'] = 3;
    }
    $res = pdo_update('yzzc_sun_goods', $data, $where);
    if ($res) {
//        //下架上架车辆
//        if ($data['status'] == 4){
//            pdo_update('yzzc_sun_goods', ['status' => 3], array('uniacid' => $_W['uniacid'],'store_car_id'=>$_GPC['car_id']));
//        }
        message('操作成功', $this->createWebUrl('goods_apply', array()), 'success');
    } else {
        message('操作失败', $this->createWebUrl('goods_apply', array()), 'error');
    }
}
elseif ($op == 'shelves') {
    //上架

    $info = pdo_get('yzzc_sun_goods', ['uniacid' => $_W['uniacid'], 'id' => $_GPC['id']]);
    $info['sku'] = pdo_getall('yzzc_sun_goods_sku',['uniacid' => $_W['uniacid'], 'goods_id' => $_GPC['id']],['id','carnum','colour','displacement']);

    $cartype = pdo_getall('yzzc_sun_cartype', ['uniacid' => $_W['uniacid']], ['id', 'name']);

    $car_brand = pdo_getall('yzzc_sun_carbrand', ['uniacid' => $_W['uniacid']], ['id', 'name']);
    $brand_list = pdo_getall('yzzc_sun_branch', ['uniacid' => $_W['uniacid']], array('id', 'name'));
    $car_tag = pdo_getall('yzzc_sun_car_tag', array('uniacid' => $_W['uniacid']));
    $store_info = pdo_get('yzzc_sun_store', ['uniacid' => $_W['uniacid'], 'store_id'=>$info['store_id']]);
    if (checksubmit('submit')) {
        if (!$_GPC['sid']) {
            message('请选择上架指定门店', '', 'error');
        }
        if ($_GPC['name'] == null) {
            message('请您车辆名称', '', 'error');
        }
        if (!isset($_GPC['carnum']) || !$_GPC['carnum']) {
            message('请您填写车牌号码', '', 'error');
        }
        if ($_GPC['pic'] == null) {
            message('请您写上传图片', '', 'error');
            die;
        }
        if (!$_GPC['money']){
            message('请您填写租金', '', 'error');
            die;
        }
        $goods_id = $_GPC['id'];
        $data = [
            'uniacid' => $_W['uniacid'],
            'sid' => $_GPC['sid'],
            'name' => $_GPC['name'],
            'structure' => $_GPC['structure'],
            'grarbox' => $_GPC['grarbox'],
            'depositway' => $_GPC['depositway'],
            'deposit' => $_GPC['deposit'],
            'money' => $_GPC['money'],
//            'act_money' => $_GPC['act_money'],
            'cartype' => $_GPC['cartype'],
            'content' => $_GPC['content'],
            'fee' => $_GPC['fee'],
            'service_fee' => $_GPC['service_fee'],
            'hot' => $_GPC['hot'],
            'num' => $_GPC['num'],
            'pic' => $_GPC['pic'],
            'zx_service_fee' => $_GPC['zx_service_fee'],
            'brand_id' => $_GPC['brand_id'],
            'open_xz' => $_GPC['open_xz'],
            'platform_fee' => $_GPC['platform_fee'],
            'status' => 1,
            'updatetime' => time()
        ];

        if (!empty($_GPC['tag_id'])) {
            $data['tag_id'] = implode(',', $_GPC['tag_id']);
        }

        if ($_GPC['subscribe_duration']) {
            $data['subscribe_duration'] = implode(',', $_GPC['subscribe_duration']);
        }
        $goods = pdo_get('yzzc_sun_goods', array('id' =>$goods_id, 'uniacid' => $_W['uniacid']));
        $res = pdo_update('yzzc_sun_goods', $data, array('id' =>$goods_id, 'uniacid' => $_W['uniacid']));
        foreach ($_GPC['sku_id'] as $key => $value) {
            if ($value){
                $sku = pdo_get('yzzc_sun_goods_sku',['id'=>$value]);
                $sku = [
                    'carnum' => $_GPC['carnum'][$key],
                    'colour' => $_GPC['colour'][$key],
                    'displacement' => $_GPC['displacement'][$key],
                ];
                $sku = array_merge($sku, $data);
                pdo_update('yzzc_sun_goods_sku', $sku,['id'=>$value, 'goods_id' => $_GPC['id']]);
            }else{
                $sku = [
                    'carnum' => $_GPC['carnum'][$key],
                    'colour' => $_GPC['colour'][$key],
                    'displacement' => $_GPC['displacement'][$key],
                    'goods_id' => $_GPC['id'],
                    'store_id' => $goods['store_id'],
                    'store_name' => $goods['store_name'],
                    'source_type' => $goods['source_type'],
                ];
                $sku = array_merge($sku, $data);
                pdo_insert('yzzc_sun_goods_sku', $sku);
            }
        }
        if ($res) {
            if ($data['subscribe_duration']) {
                pdo_delete('yzzc_sun_specprice', array('uniacid' => $_W['uniacid'], 'gid' => $_GPC['id']));
                $newData = [];
                foreach ($_GPC['price'] as $k => $v) {
                    foreach ($_GPC['subscribe_duration'] as $kk => $vv) {
                        if ($k == $kk) {
                            $newData = [
                                'gid' => $_GPC['id'],
                                'price' => $v,
                                'spec' => $vv,
                                'uniacid' => $_W['uniacid']
                            ];
                            if ($newData['price']) {
                                pdo_insert('yzzc_sun_specprice', $newData);
                            } else {
                                message('请输入对应活动价格！');
                            }

                        }
                    }
                }
            }
        }
        if ($res) {
            message('成功', $this->createWebUrl('goods', array()), 'success');
        } else {
            message('失败', '', 'error');
        }
    }
//    if (checksubmit('submit')) {
//        if ($_GPC['money'] == null) {
//            message('请输入车辆租金', '', 'error');
//        }
//        $data = [
//            'store_car_id' => $info['car_id'],
//            'store_id' => $info['store_id'],
//            'store_name' => $info['store_name'],
//            'source_type' => $info['store_name'],
//            'structure' => $branch_info['type'],
//            'grarbox' => $_GPC['grarbox'],
//            'depositway' => $_GPC['depositway'],
//            'deposit' => $_GPC['deposit'],
//            'uniacid' => $_W['uniacid'],
//            'sid' => $branch_info['id'],
//            'money' => $_GPC['money'],
//            'act_money' => $_GPC['act_money'],
//            'cartype' => $_GPC['cartype'],
//            'content' => $_GPC['content'],
//            'fee' => $_GPC['fee'],
//            'service_fee' => $_GPC['service_fee'],
//            'zx_service_fee' => $_GPC['zx_service_fee'],
//            'hot' => $_GPC['hot'],
//            'pic' => $_GPC['pic'],
//            'brand_id' => $_GPC['brand_id'],
//            'open_xz' => $_GPC['open_xz'],
//            'name' => $_GPC['name']
//        ];
//
//        if (!empty($_GPC['tag_id'])) {
//            $data['tag_id'] = implode(',', $_GPC['tag_id']);
//        }
//
//        if ($_GPC['subscribe_duration']) {
//            $data['subscribe_duration'] = implode(',', $_GPC['subscribe_duration']);
//        }
//
//        if (empty($_GPC['id'])) {
//            //新增
//            $data['createtime'] = date('Y-m-d H:i:s', time());
//            $res = pdo_insert('yzzc_sun_goods', $data, array('uniacid' => $_W['uniacid']));
//            $goods_id = pdo_insertid();
//            //添加sku
//            foreach ($_GPC['carnum'] as $key => $value) {
//
//                $sku = [
//                    'carnum' => $value,
//                    'colour' => $_GPC['colour'][$key],
//                    'displacement' => $_GPC['displacement'][$key],
//                    'goods_id' => $goods_id
//                ];
//                $sku = array_merge($sku, $data);
//                pdo_insert('yzzc_sun_goods_sku', $sku);
//            }
//            $gid = pdo_insertid();
//            if ($res) {
//                pdo_update('yzzc_sun_store_car',['status' => 2],['uniacid' => $_W['uniacid'],'car_id'=>$data['store_car_id']]);
//                if ($data['subscribe_duration']) {
//                    $newData = [];
//                    foreach ($_GPC['price'] as $k => $v) {
//                        foreach ($_GPC['subscribe_duration'] as $kk => $vv) {
//                            if ($k == $kk) {
//                                $newData = [
//                                    'gid' => $gid,
//                                    'price' => $v,
//                                    'spec' => $vv,
//                                    'uniacid' => $_W['uniacid']
//                                ];
//                                if ($newData['price']) {
//                                    pdo_insert('yzzc_sun_specprice', $newData);
//                                    // message('添加成功',$this->createWebUrl('goods',array()),'success');
//                                } else {
//                                    message('请输入对应活动价格！');
//                                }
//
//                            }
//                        }
//                    }
//                }
//            }
//        } else {
//            //修改
//            $data['status'] = 1;
//            $data['updatetime'] = time();
//            $res = pdo_update('yzzc_sun_goods', $data, array('store_car_id' => $_GPC['store_car_id'], 'uniacid' => $_W['uniacid']));
//            if ($res) {
//                //上架商家车辆
//                pdo_update('yzzc_sun_store_car',['status' => 2],['uniacid' => $_W['uniacid'],'car_id'=>$data['store_car_id']]);
//                if ($data['subscribe_duration']) {
//                    pdo_delete('yzzc_sun_specprice', array('uniacid' => $_W['uniacid'], 'gid' => $_GPC['id']));
//                    $newData = [];
//                    foreach ($_GPC['price'] as $k => $v) {
//                        foreach ($_GPC['subscribe_duration'] as $kk => $vv) {
//                            if ($k == $kk) {
//                                $newData = [
//                                    'gid' => $_GPC['id'],
//                                    'price' => $v,
//                                    'spec' => $vv,
//                                    'uniacid' => $_W['uniacid']
//                                ];
//                                if ($newData['price']) {
//                                    pdo_insert('yzzc_sun_specprice', $newData);
//                                } else {
//                                    message('请输入对应活动价格！');
//                                }
//
//                            }
//                        }
//                    }
//                }
//            }
//        }
//        if ($res) {
//
//            message('成功', $this->createWebUrl('goods', array()), 'success');
//        } else {
//            message('失败', '', 'error');
//        }
//    }

    include $this->template('web/store/store_car_shelves');
}
elseif ($op == 'agent_shelves'){
    $res = pdo_update('yzzc_sun_goods', ['status'=>1], array('uniacid' => $_W['uniacid'],'store_car_id'=>$_GPC['car_id']));
    $res1 = pdo_update('yzzc_sun_store_car', ['status'=>2], array('uniacid' => $_W['uniacid'],'car_id'=>$_GPC['car_id']));
    if ($res1 && $res){
        message('成功', $this->createWebUrl('storecar', array()), 'success');
    }else {
        message('失败', '', 'error');
    }
}
